<?php
namespace Foam\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Sql;

class ProduitTable extends AbstractTableGateway
{
    protected $table ='foam_produit';
    
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Produit());
        $this->initialize();
    }
    
    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }

    public function getProduit($id)
    {
        $id  = (int) $id;
        $rowset = $this->select(array('id_produit' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }
    
    public function getList()
    {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array(
            'id_produit', 
            'libelle',
            'code'
        ));
        $select->from(array('prd' => $this->table));
        $select->join(array('f' => 'foam_famille'), 
                        'prd.id_famille = f.id_famille',
                        array('flibelle' => 'libelle'));
        $select->order('code ASC');
        
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        return $result;
    }
    
    public function getAll()
    {
        $adapter = $this->adapter;
        $sql = new Sql($adapter);
        $select = $sql->select();
        
        $select->from(array('prd' => $this->table));
        $select->join(array('xt' => 'foam_production'), 
                        'prd.id_production = xt.id_production',
                        array('xtlibelle' => 'libelle'));
        $select->order('code ASC');
        
        $selectString = $sql->getSqlStringForSqlObject($select);
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }
    
    public function saveNew($id_produit,$libelle,$id_categorie,$id_famille,$id_ssfamille,$id_gamme,$id_ssgamme,$code,$prix_base,$presence_ctv,$presence_louvre,$livraison,$prix_figaro,$food_paper_cost,$marge,$id_production,$target,$serie_10,$fiche_tek_hermes,$fiche_tek_formation,$certification_bio,$presence_lactose,$id_contenant,$id_dlc,$id_service,$a_savoir,$temps_presence,$ingredients_etiquette,$produit_complementaire,$prix_printemps,$tva_sp,$tva_vae,$prix_concession,$volumes_estimes,$image,$actif,$date_actif)
    {        
        $data = array(
            'id_produit' => $id_produit,
            'libelle' => $libelle,
            'id_categorie' => $id_categorie,
            'id_famille' => $id_famille,
            'id_ssfamille' => $id_ssfamille,
            'id_gamme' => $id_gamme,
            'id_ssgamme' => $id_ssgamme,
            'code' => $code,
            'prix_base' => $prix_base,
            'presence_ctv' => $presence_ctv,
            'presence_louvre' => $presence_louvre,
            'livraison' => $livraison,
            'prix_figaro' => $prix_figaro,
            'food_paper_cost' => $food_paper_cost,
            'marge' => $marge,
            'id_production' => $id_production,
            'target' => $target,
            'serie_10' => $serie_10,
            'fiche_tek_hermes' => $fiche_tek_hermes,
            'fiche_tek_formation' => $fiche_tek_formation,
            'certification_bio' => $certification_bio,
            'presence_lactose' => $presence_lactose,
            'id_contenant' => $id_contenant,
            'id_dlc' => $id_dlc,
            'id_service' => $id_service,
            'a_savoir' => $a_savoir,
            'temps_presence' => $temps_presence,
            'ingredients_etiquette' => $ingredients_etiquette,
            'produit_complementaire' => $produit_complementaire,
            'prix_printemps' => $prix_printemps,
            'tva_sp' => $tva_sp,
            'tva_vae' => $tva_vae,
            'prix_concession' => $prix_concession,
            'volumes_estimes' => $volumes_estimes,
            'image' => $image,
            'actif' => $actif,  
            'date_actif' => $date_actif,
        );
        
        $this->insert($data);
        return $this->lastInsertValue;
        //return $this->getAdapter()->lastInsertId();
    }
    
    public function saveUpdate($id_produit,$libelle,$id_categorie,$id_famille,$id_ssfamille,$id_gamme,$id_ssgamme,$code,$prix_base,$presence_ctv,$presence_louvre,$livraison,$prix_figaro,$food_paper_cost,$marge,$id_production,$target,$serie_10,$fiche_tek_hermes,$fiche_tek_formation,$certification_bio,$presence_lactose,$id_contenant,$id_dlc,$id_service,$a_savoir,$temps_presence,$ingredients_etiquette,$produit_complementaire,$prix_printemps,$tva_sp,$tva_vae,$prix_concession,$volumes_estimes,$image,$actif,$date_actif)
    {        
        $data = array(
            'libelle' => $libelle,
            'id_categorie' => $id_categorie,
            'id_famille' => $id_famille,
            'id_ssfamille' => $id_ssfamille,
            'id_gamme' => $id_gamme,
            'id_ssgamme' => $id_ssgamme,
            'code' => $code,
            'prix_base' => $prix_base,
            'presence_ctv' => $presence_ctv,
            'presence_louvre' => $presence_louvre,
            'livraison' => $livraison,
            'prix_figaro' => $prix_figaro,
            'food_paper_cost' => $food_paper_cost,
            'marge' => $marge,
            'id_production' => $id_production,
            'target' => $target,
            'serie_10' => $serie_10,
            'fiche_tek_hermes' => $fiche_tek_hermes,
            'fiche_tek_formation' => $fiche_tek_formation,
            'certification_bio' => $certification_bio,
            'presence_lactose' => $presence_lactose,
            'id_contenant' => $id_contenant,
            'id_dlc' => $id_dlc,
            'id_service' => $id_service,
            'a_savoir' => $a_savoir,
            'temps_presence' => $temps_presence,
            'ingredients_etiquette' => $ingredients_etiquette,
            'produit_complementaire' => $produit_complementaire,
            'prix_printemps' => $prix_printemps,
            'tva_sp' => $tva_sp,
            'tva_vae' => $tva_vae,
            'prix_concession' => $prix_concession,
            'volumes_estimes' => $volumes_estimes,
            'image' => $image,
            'actif' => $actif,  
            'date_actif' => $date_actif,
        );
        
        $this->update($data, array('id_produit' => $id_produit));
    }
    
    public function deleteProduit($id)
    {
        $this->delete(array('id_produit' => $id));
    }
}
